// ===================================================
// wwMenu Class
// ===================================================
function wwMenuDataModel(oData)
{
	this.oData = oData;
	this.oMenu = null;
	
	// roots
	this.buildModel = function (oMenu)
	{
		// regenerate menu
		oMenu.removeAll();
		
		var m = new BiMenu;
		var mdel = new BiMenuItem("Delete");
		mdel.setIcon(new BiImage("images/delete.png"));
		mdel.addEventListener("action", function (e) 
		{
			// Investigar como sacar el id
			// cuando se clickea en delete del menu contextual
			
			//var c = e.getTarget();
			//alert(c.getComponent());
			
			var win = application.getComponentById("viewDelete");
			var tmp = _p.addWindow(win);
			tmp.setActive(true);
		});
		m.add( mdel );

		oMenu.addEventListener("contextmenu", function (e) 
		{
			m.setLocation(e.getScreenX(), e.getScreenY());
			m.setVisible(true);
		});

		var oData = this.oData;

		// iterate root... for the moment there is one root		
		for(var p in oData)
		{
			// if it's a folder
			if (oData[p].vURL == null)
			{
				// if it has childs
				if (oData[p].childs != null)
				{
					var oChildMenu = this.buildChild(oData[p].childs);
				}
				else
				{
					var oChildMenu = new BiMenu;
				}
				
				// Change menu
				
				// separator
				var oChildSeparator = new BiMenuSeparator();

				// new folder
				var oChildFolder = new BiMenuItem("Add folder");
				oChildFolder.setIcon(new BiImage("images/folder_add.png"));
				oChildFolder.iLnkID = oData[p].iLnkID;
				oChildFolder.addEventListener("action", function (e) 
				{
					var parentid = application.getComponentById("iRelParentID");
					parentid.setText(e.getTarget().iLnkID);
					var win = application.getComponentById("viewFolder");
					var tmp = _p.addWindow(win);
					tmp.setActive(true);
				});
				
				// new link				
				var oChildLink = new BiMenuItem("Add link");
				oChildLink.setIcon(new BiImage("images/link_add.png"));
				oChildLink.iLnkID = oData[p].iLnkID;
				// command
				oChildLink.addEventListener("action", function (e) 
				{
					var parentid = application.getComponentById("iRelParentIDLink");
					parentid.setText(e.getTarget().iLnkID);
					var win = application.getComponentById("viewLink");
					var tmp = _p.addWindow(win);
					tmp.setActive(true);
				});
				
				oChildMenu.add(oChildSeparator);
				oChildMenu.add(oChildFolder);
				oChildMenu.add(oChildLink);
				
				// Enter
				var oChild = new BiMenuButton(oData[p].vLnkName, oChildMenu);
				oChild.setIcon(new BiImage("images/star.png"));
			}
			else
			{
				var oChild = new BiMenuItem(oData[p].vLnkName);
				oChild.setIcon(new BiImage("images/application_xp.png"));
				oChild.vURL = oData[p].vURL;
				oChild.vLnkName = oData[p].vLnkName;
				// command
				oChild.addEventListener("action", function (e) 
				{
					var win = new BiWindow(e.getTarget().vLnkName);
					var tmp = _p.addWindow(win, e.getTarget().vURL);
					tmp.setActive(true);
				});
			}
			
			oMenu.add(oChild);
		}
	}
	
	// childs	
	this.buildChild = function (oChilds)
	{
		var oMenu = new BiMenu;	
		
		for(var p in oChilds)
		{
			// if it's a folder
			if (oChilds[p].vURL == null || oChilds[p].vURL == '')
			{
				// if it has childs
				if (oChilds[p].childs != null)
				{
					var oChildMenu = this.buildChild(oChilds[p].childs);
				}
				else
				{
					var oChildMenu = new BiMenu;
				}
				
				// separator
				var oChildSeparator = new BiMenuSeparator();
				
				// new folder
				var oChildFolder = new BiMenuItem("Add folder");
				oChildFolder.setIcon(new BiImage("images/folder_add.png"));
				oChildFolder.iLnkID = oChilds[p].iLnkID;
				oChildFolder.addEventListener("action", function (e) 
				{
					var parentid = application.getComponentById("iRelParentID");
					parentid.setText(e.getTarget().iLnkID);
					var win = application.getComponentById("viewFolder");
					var tmp = _p.addWindow(win);
					tmp.setActive(true);
				});
				
				// new link				
				var oChildLink = new BiMenuItem("Add link");
				oChildLink.setIcon(new BiImage("images/link_add.png"));
				oChildLink.iLnkID = oChilds[p].iLnkID;
				// command
				oChildLink.addEventListener("action", function (e) 
				{
					var parentid = application.getComponentById("iRelParentIDLink");
					parentid.setText(e.getTarget().iLnkID);
					var win = application.getComponentById("viewLink");
					var tmp = _p.addWindow(win);
					tmp.setActive(true);
				});
				
				oChildMenu.add(oChildSeparator);
				oChildMenu.add(oChildFolder);
				oChildMenu.add(oChildLink);
				
//				var oChild = new BiMenuButton(oChilds[p].vLnkName, oChildMenu);
				var oChild = new BiMenuItem(oChilds[p].vLnkName, oChildMenu);
				oChild.setIcon(new BiImage("images/folder.png"));
			}
			else
			{
				var oChild = new BiMenuItem(oChilds[p].vLnkName);
				oChild.setIcon(new BiImage("images/application_xp.png"));
				// todo: this is magic... but dark magic
				oChild.vURL = oChilds[p].vURL;
				oChild.vLnkName = oChilds[p].vLnkName;
				// command
				oChild.addEventListener("action", function (e) 
				{
					var win = new BiWindow(e.getTarget().vLnkName);
					var tmp = _p.addWindow(win, e.getTarget().vURL);
					tmp.setActive(true);
				});
			}
			
			oMenu.add(oChild);
			
			
		}
		
		return oMenu;
	}
}



// ===================================================
// wwSocialMenu Class
// ===================================================
function wwSocialMenu()
{
	// get the social menu from server
	this.getSocialUrl = function (oMenu, idCategory)
	{
		var loader = new BiJsonLoader();
		loader.addEventListener("load", function(e)
		{
			var oChilds = e.getTarget().getData();
			
			// Refactory! this class must extends wwMenu and this method
			// would be called addChilds
			for(var p in oChilds)
			{
				var oChild = new BiMenuItem(oChilds[p].vLnkName);
				oChild.setIcon(new BiImage("images/application_xp.png"));
				// todo: this is magic... but dark magic
				oChild.vURL = oChilds[p].vURL;
				oChild.vLnkName = oChilds[p].vLnkName;
				// command
				oChild.addEventListener("action", function (e) 
				{
					var win = new BiWindow(e.getTarget().vLnkName);
					var tmp = _p.addWindow(win, e.getTarget().vURL);
					tmp.setActive(true);
				});
					
				oMenu.add(oChild);
			}									
			
		}, loader);
		
		// get social url for the category
		loader.load("getsocialsurl.php?id=" + idCategory);
	}
	
	// add link
	this.addLink = function(iSocialID)
	{
		// Add link button
		var oChildLink = new BiMenuItem("Add link");
		oChildLink.setIcon(new BiImage("images/link_add.png"));
		oChildLink.iLnkID = iSocialID;

		// command
		oChildLink.addEventListener("action", function (e) 
		{
			var parentid = application.getComponentById("iSocialID");
			parentid.setText(e.getTarget().iLnkID);
			var win = application.getComponentById("viewSocialLink");
			var tmp = _p.addWindow(win);
			tmp.setActive(true);
		});	
		
		return oChildLink;	
	}
	
	// add social urls
	this.addSocialURLs = function(iSocialID)
	{
		var oChildMenu = new BiMenu;

		// add add link		
		oChildMenu.add(this.addLink(iSocialID));
		var oChildSeparator = new BiMenuSeparator();
		oChildMenu.add(oChildSeparator);
						
		// add socials urls
		this.getSocialUrl(oChildMenu, iSocialID);
		
		return oChildMenu;
	}
	
	// add link
	this.addSuggests = function()
	{
		var oChildLink = new BiMenuItem("Suggests category");
		oChildLink.setIcon(new BiImage("images/feed.png"));
		
		return oChildLink;
	}	
	
	// add category
	this.addCategory = function(catName, icon, iSocialID)
	{
		var oChildLink = new BiMenuItem(catName, this.addSocialURLs(iSocialID));
		if (icon != null) oChildLink.setIcon(new BiImage("images/" + icon));
		
		return oChildLink;
	}
	
	// buildCategories
	this.buildCategories = function()
	{
		var oCategories = new BiMenu;
		oCategories.add(this.addCategory('Games', 'cup.png', 1));
		oCategories.add(this.addCategory('Search', 'zoom.png', 2));
		oCategories.add(this.addCategory('Maps', 'world.png', 3));
		oCategories.add(this.addCategory('Newspapers', 'newspaper.png', 4));
		oCategories.add(this.addCategory('Music', 'music.png', 5));
		oCategories.add(this.addCategory('Broadcast', 'television.png', 6));
		oCategories.add(this.addCategory('Photos', 'photo.png', 7));
		//oCategories.add(this.addCategory('Adult sites', 'heart.png', 8));
		oCategories.add(this.addCategory('Web applications', 'cog.png', 9));		

		// suggests	
		//oCategories.add(this.addSuggests());
				
		return oCategories;
	}
	
	// social menu
	this.addSocialMenu = function ()
	{
		// agregar sugerir categoria
		//var oChild = new BiMenuButton("Social menu", this.buildCategories());
		var oChild = new BiMenuButton("Social menu", this.buildCategories());
		oChild.setIcon(new BiImage("images/world.png"));
		
		return oChild;
	}
}


// ===================================================
// wwYouTube Menu
// ===================================================
function wwYouTube()
{
	// build menu...
	this.buildMenu = function()
	{
		// global
    oYouTubeMenu = new BiMenu;
    getTopTenVideos();
		// suggests	
		//oCategories.add(this.addSuggests());
		return oYouTubeMenu;
	}
	
	// social menu
	this.addYouTube = function ()
	{
		var oChild = new BiMenuButton("YouTube TopFavorites", this.buildMenu());
		oChild.setIcon(new BiImage("images/youtube.ico"));
		
		return oChild;
	}
}